using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Facebook.Service;
using AjaxNinja.Data;
using AjaxNinja.Types;
using AjaxNinja.Business;
using AjaxNinja.Debug;
using Snap.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    private InputProcessor p;
    private SQLDb db;
    private Diagnostics dg;
    private int userID;

    /// <summary>
    /// Loading routine for the latest thing that pissed
    /// someone off today.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.DataBind(); //Bind the data (force the cobwebs out of the Resources files)
        userID = Int32.Parse(Master.fbApp.UserID);

        //If someone submitted form data for adding a new message
        if (this.IsPostBack)
        {
            p = new InputProcessor();
            if (Debugging.DebugOutput) //Debug output
            {
                Response.Write("<div class=\"debug\">Postback Detected</div>");
            }

            MadMessage newInput = new MadMessage();
            newInput.msg = Server.HtmlEncode(this.an_mad_text.Text);
            AppUser user = new AppUser();
            user.userID = this.userID;
            String processResult = p.ProcessInput(newInput, user);
            this.postBackMessages.Text = processResult;

            if (Debugging.DebugOutput) //Debug output
            {
                Response.Write("<div class=\"debug\">Message Processed; Received:<br>");
                Response.Write(Server.HtmlEncode(processResult));
                Response.Write("</div>");
            }
        }

        if (Debugging.DebugOutput) //Debug output
        {
            Response.Write("<div class=\"debug\">User ID " + Master.fbApp.UserID + " detected.</div>");
            if (this.IsPostBack)
            {
                Response.Write("<div class=\"debug\">Postback detected.</div>");
            }
            else
            {
                Response.Write("<div class=\"debug\">Postback not detected.</div>");
            }
        }

        //Get our SQL Data writer ready
        db = new SQLDb();
        dg = new Diagnostics("fbConnectionString");       

        try
        {
            MadMessage msg = this.db.GetLastMessage(userID);
            if (msg == null || msg.Equals(default(MadMessage))) //If we have no data
            {
                if (Debugging.DebugOutput)
                {
                    Response.Write("<div class=\"debug\">No Updates Found</div>");
                }
                this.RelativeTimeDifference.Text = "no updates";
                this.RelativeTimeDifference.UseTextInsteadOfDates = true;
                this.lastMessage.Text = "";
            }
            else
            {
                if (Debugging.DebugOutput)
                {
                    Response.Write("<div class=\"debug\">Recent Updates Detected</div>");
                }
                this.RelativeTimeDifference.StartTime = msg.timeStamp;
                this.RelativeTimeDifference.EndTime = System.DateTime.Now;
                this.RelativeTimeDifference.UseTextInsteadOfDates = false;
                this.lastMessage.Text = msg.msg;
            }
        }
        catch (Exception er)
        {
            SError logMsg = new SError("SQLDb.cs", er.TargetSite.Name, "", 0, er.Message + er.StackTrace);
            dg.logError(logMsg);
        }
    }
}
